﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
using System.Collections.Generic;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public partial class VVAKDBEZ
    {
        public readonly static VVAKDBEZ Instance = new VVAKDBEZ();
        VGSCOLOR GSC;
        VKDSTAMM KD;
        private DataRow _dwVAK = null;
        VVAFARBEN _VAF;
        public VVAFARBEN VAF
        {
            get
            {
                if (_VAF == null) _VAF = new VVAFARBEN();
                return _VAF;
            }
        }
        VGSMATK _GSM;
        public VGSMATK GSM
        {
            get
            {
                if (_GSM == null) _GSM = new VGSMATK();
                return _GSM;
            }
        }
        VSDDAT _SD;
        public VSDDAT SD
        {
            get
            {
                if (_SD == null) _SD = new VSDDAT();
                return _SD;
            }
        }
        public DataRow dwVAK
        {
            get { return _dwVAK; }
            set
            {
                _dwVAK = value;
                OnPropertyChanged("dwVAK", value);
            }
        }

        public void STUVAKDBEZ()
        {
            SResult = QueryVAKDBEZ(dwData, -1);
            MergeData(SResult, true);
        }
        public void NEWVAKDBEZ(Dictionary<string, object> pm)
        {
            dwData = TBMain.NewRow();
            SetUpNewRow(ref dwData, null);

            dwData["VAKBKOLL"] =Helper.Left( pm["KOLL"].ToString().PadRight(5),5);
            dwData["VAKBART"] = Helper.Left(pm["ART"].ToString().PadRight(24), 24);  
            dwData["VAKBKUNDNR"] = Helper.Left(pm["KUNDNR"].ToString().PadRight(8), 8);
            dwData["VAKBFARBE"] = Helper.Left(pm["FARBE"].ToString().PadRight(10), 10);
            dwData["CR"] = 0;
            dwData["CG"] = 0;
            dwData["CB"] = 0;
            Insert(dwData);
        }
        public void Insert(DataRow dwVAK)
        {
            if (dwVAK == null)
                return; 
            dwVAK["VAKBNEUBEN"] = app.User;
            dwVAK["VAKBNEUDAT"] = Helper.Right("00" + day, 2) + Helper.Right("00" + month, 2) + Helper.Right(year, 2);
            DeleteVAKDBEZ(dwVAK, 1);
            InsertVAKDBEZ(dwVAK, app.company);
            TBMain.Rows.InsertAt(dwVAK, 0);
        }
        public void Delete(DataRow dwVK)
        {
            if (dwVK == null)
                return; 
            dwData = VAF.TBMain.NewRow();
            dwData["VAFMANDANT"] = app.Mandant;
            dwData["VAFKOLL"] = dwVK["VAKBKOLL"];
            dwData["VAFART"] = dwVK["VAKBART"];
            dwData["VAFFARBE"] = dwVK["VAKBFARBE"];            
            VAF.DVF01(dwData);
            dwData = GSM.TBMain.NewRow();
            dwData["GSMMANDANT"] = app.Mandant;
            dwData["GSMART"] = Helper.Left(dwVK["VAKBFARBE"].ToString().PadRight(20),20);
            GSM.DGSM02(dwData);
            dwData = SD.TBMain.NewRow();
            dwData["SDSA"] = "0014";
            dwData["SDKEY"] = "00" + Helper.Left(dwVK["VAKBFARBE"].ToString().PadRight(21), 21) + "V";
            SD.DSD01(dwData);
            dwData["SDSA"] = "0014";
            dwData["SDKEY"] = "00" + Helper.Left(dwVK["VAKBFARBE"].ToString().PadRight(21), 21) + "E";
            SD.DSD01(dwData);
            dwData["SDSA"] = "0014";
            dwData["SDKEY"] = "00" + Helper.Left(dwVK["VAKBFARBE"].ToString().PadRight(21), 21) + " ";
            SD.DSD01(dwData);
            DeleteVAKDBEZ(dwVK, 1);
            string filter = " VAKBFARBE='" + dwVK["VAKBFARBE"].ToString() + "' AND VAKBKOLL='" + dwVK["VAKBKOLL"].ToString() + "' AND VAKBART ='" + dwVK["VAKBART"].ToString() + "' AND VAKBKUNDNR='" + dwVK["VAKBKUNDNR"].ToString() + "'";
            DataRow[] rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
        }

        public void VAKDCHK(Dictionary<string, object> pm)
        { 
            MessageInfo msg = new MessageInfo();
            msg.MSGTYPE = "VAKDCHK";
            msg.Sender = pm["RUNID"].ToString();
            msg.ACTION = "VAKDCHK";
            msg.Sender = pm["RUNID"].ToString();
            msg.KOLL = pm["KOLL"].ToString();
            msg.ART = pm["ART"].ToString() ;
            msg.FARBE = pm["FARBE"].ToString() ;
            msg.P1 = pm["KUNDER"].ToString() ;
            msg.Where = app.User;
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

        public void VAKDFARBECHG(Dictionary<string, object> pm)
        {
            MessageInfo msg = new MessageInfo();
            msg.MSGTYPE = "VAKDFARBECHG";
             msg.ACTION = "VAKDFARBECHG";
            msg.Sender = pm["RUNID"].ToString();
            msg.KOLL = pm["KOLL"].ToString();
            msg.ART = pm["ART"].ToString();
            msg.FARBE = pm["FARBE"].ToString();
            msg.P1 = pm["KUNDER"].ToString();
            msg.P2 = pm["NFARBE"].ToString();
            msg.Where = app.User;
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }
    }
}